package com.okhttp.comment;

import android.app.Activity;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.baselibrary.bean.BottomDialogBean;
import com.baselibrary.utils.NumberUtils;
import com.baselibrary.utils.PopUtils;
import com.baselibrary.utils.ScreenUtils;
import com.baselibrary.utils.SpUtils;
import com.baselibrary.utils.TimeUtil;
import com.baselibrary.view.VipHeardImgView;
import com.like.LikeButton;
import com.like.OnLikeListener;
import com.okhttp.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 子评论布局
 */
public class BaseCommentChildListAdapter extends RecyclerView.Adapter<BaseCommentChildListAdapter.BaseCommentChildListHoder> {

    public List<CommentBean.DataBean.CommentInfoBean.ReplyCommentsBean> mList = new ArrayList<>();

    private CommentBean.DataBean.CommentInfoBean mMorMarListBean = null;
    private OnCommentChildClickListener mOnCommentChildClickListener;
    private Activity activity;
    public int pager = 1;

    public BaseCommentChildListAdapter(Activity activity) {
        this.activity = activity;
    }

    public void setList(List<CommentBean.DataBean.CommentInfoBean.ReplyCommentsBean> mList) {
        this.mList = mList;
        notifyDataSetChanged();
    }

    public void addList(List<CommentBean.DataBean.CommentInfoBean.ReplyCommentsBean> mList) {
        this.mList.addAll(mList);
        notifyDataSetChanged();
    }

    public void addList(CommentBean.DataBean.CommentInfoBean.ReplyCommentsBean bean) {
        this.mList.add(0, bean);
        notifyDataSetChanged();
    }

    public void setMorMarListBean(CommentBean.DataBean.CommentInfoBean mMorMarListBean) {
        this.mMorMarListBean = mMorMarListBean;
    }

    public void setOnCommentChildClickListener(OnCommentChildClickListener mOnCommentChildClickListener) {
        this.mOnCommentChildClickListener = mOnCommentChildClickListener;
    }

    public void removeData(CommentBean.DataBean.CommentInfoBean.ReplyCommentsBean replyCommentsBean) {
        mList.remove(replyCommentsBean);
        notifyDataSetChanged();
    }

    public void removeData(int pos) {
        if (mList.size() > pos) {
            mList.remove(pos);
            notifyDataSetChanged();
        }
    }

    /**
     * 本地添加一个新的 二级评论
     */
    public void addNewChildrenCommentBeans(CommentBean.DataBean.CommentInfoBean.ReplyCommentsBean replyCommentsBean) {
        this.mList.add(0, replyCommentsBean);
        this.notifyDataSetChanged();
    }

    @NonNull
    @Override
    public BaseCommentChildListHoder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View inflate = LayoutInflater.from(activity).inflate(R.layout.item_base_comment_list_child, viewGroup, false);
        return new BaseCommentChildListHoder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull BaseCommentChildListHoder baseCommentChildListHoder, int pos) {
        CommentBean.DataBean.CommentInfoBean.ReplyCommentsBean mReplyCommentsBean = mList.get(pos);
        CommentBean.DataBean.CommentInfoBean.ReplyCommentsBean.UserBean mUser = mReplyCommentsBean.getUser();
        if (mUser != null) {
            baseCommentChildListHoder.ivCommentUserIcon.setData(TextUtils.isEmpty(mUser.getIcon()) ? "" : mUser.getIcon(), mUser.getIsFans() == 1,View.GONE);
            baseCommentChildListHoder.ivCommentUserIcon.setParams((int) activity.getResources().getDimension(R.dimen.dp_24), (int) activity.getResources().getDimension(R.dimen.dp_24)
                    , (int) activity.getResources().getDimension(R.dimen.dp_12), (int) activity.getResources().getDimension(R.dimen.dp_8));
            String mPuName = mReplyCommentsBean.getPuname();
            if (!TextUtils.isEmpty(mPuName) && mReplyCommentsBean.getTypeId() != mReplyCommentsBean.getPid()) {
                String newContent = "回复 " + mPuName + "：" + mReplyCommentsBean.getContent();
                SpannableString spannableString = new SpannableString(newContent);
                spannableString.setSpan(new ForegroundColorSpan(activity.getResources().getColor(R.color.color_999999)), newContent.indexOf(mPuName), newContent.indexOf(mPuName) + mPuName.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                baseCommentChildListHoder.tvCommentContent.setText(spannableString);
            } else {
                baseCommentChildListHoder.tvCommentContent.setText(mReplyCommentsBean.getContent());
            }
            baseCommentChildListHoder.tvCommentUserName.setText(mUser.getName());
        }
        baseCommentChildListHoder.ivCommentGive.setLiked(mReplyCommentsBean.isSupported() == CommentBean.TRUE);
        //作者是否赞过
        baseCommentChildListHoder.tvCommentAuthorGive.setVisibility(mReplyCommentsBean.authorLike == CommentBean.TRUE ? View.VISIBLE : View.GONE);

        if (mReplyCommentsBean.isAuthor == CommentBean.TRUE) {
            baseCommentChildListHoder.tvCommentYouFollow.setText("作者");
            baseCommentChildListHoder.tvCommentYouFollow.setVisibility(View.VISIBLE);
        } else if (mUser != null && (mUser.isFocusFans() == CommentBean.FOLLOW_HE || mUser.isFocusFans() == CommentBean.FOLLOW_MUTUAL)) {
            baseCommentChildListHoder.tvCommentYouFollow.setText("你的关注");
            baseCommentChildListHoder.tvCommentYouFollow.setVisibility(View.VISIBLE);
        } else {
            baseCommentChildListHoder.tvCommentYouFollow.setVisibility(View.GONE);
        }

        baseCommentChildListHoder.tvCommentTime.setText(TimeUtil.fromToday(mReplyCommentsBean.getCtime()));
        baseCommentChildListHoder.tvCommentGiveSum.setVisibility(mReplyCommentsBean.getSupportNum() > 0 ? View.VISIBLE : View.INVISIBLE);
        baseCommentChildListHoder.tvCommentGiveSum.setText(NumberUtils.formatBigNum(String.valueOf(mReplyCommentsBean.getSupportNum())));
        baseCommentChildListHoder.initListener(mReplyCommentsBean, pos);
        if (mReplyCommentsBean.highlight == CommentBean.TRUE) {
            baseCommentChildListHoder.mViewCommentSubject.setVisibility(View.VISIBLE);
            BusinessLayout.setCommentProminent(baseCommentChildListHoder.mViewCommentSubject);
            mReplyCommentsBean.highlight = CommentBean.FALSE;
        } else {
            baseCommentChildListHoder.mViewCommentSubject.setVisibility(View.GONE);
        }

        baseCommentChildListHoder.tvCommentUserName.post(() -> {
            //36 上级评论的头像宽度、
            int marginSum = ScreenUtils.marginSum(activity, 14, 9, 8,12, 6, 14, 36);
            int userNameMaxWidth = ScreenUtils.getUserNameMaxWidth(activity, marginSum, baseCommentChildListHoder.ivCommentUserIcon, baseCommentChildListHoder.tvCommentYouFollow, baseCommentChildListHoder.llCommentGive);
            baseCommentChildListHoder.tvCommentUserName.setMaxWidth(userNameMaxWidth);
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }


    public class BaseCommentChildListHoder extends RecyclerView.ViewHolder {
        private VipHeardImgView ivCommentUserIcon;
        private TextView tvCommentUserName, tvCommentYouFollow, tvCommentGiveSum,
                tvCommentContent, tvCommentTime, tvCommentAuthorGive;
        private LinearLayout llCommentGive;
        private LikeButton ivCommentGive;
        private View mViewCommentSubject;

        public BaseCommentChildListHoder(@NonNull View itemView) {
            super(itemView);
            mViewCommentSubject = itemView.findViewById(R.id.view_comment_subject);
            ivCommentUserIcon = itemView.findViewById(R.id.iv_comment_user_icon);
            tvCommentUserName = itemView.findViewById(R.id.tv_comment_user_name);
            tvCommentYouFollow = itemView.findViewById(R.id.tv_comment_youFollow);
            llCommentGive = itemView.findViewById(R.id.ll_comment_give);
            ivCommentGive = itemView.findViewById(R.id.iv_comment_give);
            tvCommentGiveSum = itemView.findViewById(R.id.tv_comment_giveSum);
            tvCommentContent = itemView.findViewById(R.id.tv_comment_content);
            tvCommentTime = itemView.findViewById(R.id.tv_comment_time);
            tvCommentAuthorGive = itemView.findViewById(R.id.tv_comment_authorGive);
        }

        void initListener(CommentBean.DataBean.CommentInfoBean.ReplyCommentsBean mReplyCommentsBean, int pos) {

            itemView.setOnLongClickListener(v -> {
                if (mOnCommentChildClickListener == null)
                    return true;
                StringBuilder buffer = new StringBuilder();
                String content = mReplyCommentsBean.getContent();
                buffer.append(mReplyCommentsBean.getUser().getName());
                if (!TextUtils.isEmpty(content)) {
                    buffer.append("：")
                            .append(content.length() > 12 ? mReplyCommentsBean.getContent().substring(0, 12) : content);
                }
                ArrayList<BottomDialogBean> list = new ArrayList<>();
                list.add(new BottomDialogBean("回复", false));
                list.add(new BottomDialogBean("复制", false));
                if (SpUtils.getInstance().getIsLogin() && SpUtils.getInstance().getUserId() == mReplyCommentsBean.getUser().getId()) {
                    list.add(new BottomDialogBean("删除", true));
                } else {
                    list.add(new BottomDialogBean("举报", false));
                }
                PopUtils.INSTANCE.showBottomDialog(activity, buffer.toString(), list, pos1 -> {
                    switch (pos1) {
                        case 0:
                            mOnCommentChildClickListener.onCommentChildClick(mReplyCommentsBean);
                            break;
                        case 1:
                            mOnCommentChildClickListener.onCopyChildCommentContent(mReplyCommentsBean);
                            break;
                        case 2:
                            if (SpUtils.getInstance().getIsLogin() && SpUtils.getInstance().getUserId() == mReplyCommentsBean.getUser().getId()) {
                                mOnCommentChildClickListener.onCommentChildDelete(mReplyCommentsBean, pos);
                            } else {
                                mOnCommentChildClickListener.onReportChildComment(mReplyCommentsBean);
                            }
                            break;
                    }
                },true);
                return true;
            });

            itemView.setOnClickListener(view -> {
                if (mOnCommentChildClickListener != null)
                    mOnCommentChildClickListener.onCommentChildClick(mReplyCommentsBean);
            });

            ivCommentUserIcon.setOnClickListener(v -> {
                if (mOnCommentChildClickListener != null)
                    mOnCommentChildClickListener.onCommentChildUsrHeadClick(mReplyCommentsBean);
            });

            ivCommentGive.setOnLikeListener(new OnLikeListener() {
                @Override
                public void liked(LikeButton likeButton) {

                }

                @Override
                public void unLiked(LikeButton likeButton) {

                }
            });
            ivCommentGive.setOnLikeListener(new OnLikeListener() {
                @Override
                public void liked(LikeButton likeButton) {
                    onClickGive(mReplyCommentsBean);
                }

                @Override
                public void unLiked(LikeButton likeButton) {
                    onClickGive(mReplyCommentsBean);
                }
            });
            /*
            llCommentGive.setOnClickListener(v -> {

            });*/
        }

        private void onClickGive(CommentBean.DataBean.CommentInfoBean.ReplyCommentsBean mReplyCommentsBean) {
            if (mOnCommentChildClickListener != null)
                mOnCommentChildClickListener.onCommentChildGive(mReplyCommentsBean, tvCommentGiveSum, ivCommentGive);
            if (mReplyCommentsBean.getAuthUser() != null) {
                if (SpUtils.getInstance().getLoadUserBean().getData().getId() == mReplyCommentsBean.getAuthUser().getId()) {
                    mReplyCommentsBean.authorLike = mReplyCommentsBean.authorLike == CommentBean.FALSE ? CommentBean.TRUE : CommentBean.FALSE;
                    //作者是否赞过
                    tvCommentAuthorGive.setVisibility(mReplyCommentsBean.authorLike == CommentBean.TRUE ? View.VISIBLE : View.GONE);
                }
            }
        }
    }
}
